package com.neuedu.oasystem.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neuedu.oasystem.dao.IDepartmentDao;
import com.neuedu.oasystem.entity.Department;

import com.neuedu.oasystem.util.DBHelper;

public class DepartmentDaoImpl extends BaseDao implements IDepartmentDao {

	@Override
	public boolean add(Department param) {
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		try {
			String sql = "insert into depatment (dept_Id,dept_Name,dept_Information) values(dept_id_seq.nextval,?,?)";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, param.getDeptName());
			stmt.setString(2, param.getDeptInfor());
			int count = stmt.executeUpdate();
			if (count == 1) {
				return true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, stmt, conn);
		}
		return false;
	}

	@Override
	public boolean edit(Department param) {
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		try {
			String sql = "update depatment set dept_Name=?,dept_Information=?  where dept_id=?";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(3, param.getDeptId());
			stmt.setString(1, param.getDeptName());
			stmt.setString(2, param.getDeptInfor());

			int count = stmt.executeUpdate();
			if (count == 1) {
				return true;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, stmt, conn);
		}
		return false;
	}

	@Override
	public boolean deleteById(int id) {
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;

		try {
			String sql = "delete from depatment where dept_Id=?";
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, id);
			int count = stmt.executeUpdate();
			if (count == 1) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, stmt, conn);
		}

		return false;
	}

	@Override
	public Department findById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Department> findAll() {
		List<Department> list = new ArrayList<Department>();
		Connection conn = DBHelper.openConnection();
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			conn = getConnection();
			String sql = "select dept_Id,dept_Name,dept_Information from depatment ";
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			while (rs.next()) {

				int deptId = rs.getInt("dept_Id");
				String deptName = rs.getString("dept_Name");

				String deptInfor = rs.getString("dept_Information");

				Department department = new Department(deptId, deptName, deptInfor);
				list.add(department);

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(rs, stmt, conn);
		}

		return list;
	}

	@Override
	public List<Department> findByExample(Department param) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Department> findByPager(int page, int size) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deleteByIds(int[] ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int addAll(List<Department> useparamrs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int editAll(List<Department> param) {
		// TODO Auto-generated method stub
		return 0;
	}

}
